<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Password extends Console_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('admin_model');
	}
	
	public function index(){
		$gets = $this->input->get();
		$where = [];
		$assign = [
			'gets' => $gets,
			'options' => [
				'group' => $this->admin_model->group()
			]
		];
		$total = $this->admin_model->get_total($where);
		if($total > 0){
			$this->load->library('pagination', [
				'total_num' => $total
			]);
			$assign['show_page'] = $this->pagination->create();
			$assign['list'] = $this->admin_model->get_result([
				'where' => $where,
				'start' => $this->pagination->limit_start,
				'limit' => $this->pagination->page_size
			]);
		}
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('password.htm');
	}
	
	public function submit(){
		$posts = $this->input->post();
		if(!$posts){
			$this->output_json();
		}
		$oldpwd = $posts['old_passwd'];
		$password = $posts['password'];
		$confirm = $posts['confirm_passwd'];
		if($password != $confirm){
			$this->output_json('确认密码和新密码不一致');
		}
		$this->load->helper('common');
		$res = $this->admin_model->get_row([
			'id' => $this->uid,
			'password' => mmd5($oldpwd)
		]);
		if(!$res){
			$this->output_json('旧密码不正确');
		}
		$query = $this->admin_model->set([
			'password' => mmd5($password)
		], $this->uid);
		if($query){
			$this->output_json('提交成功', 0);
		}
		$this->output_json('提交失败');
	}
	
}